package dao;

import util.DBHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReturnRequestDao {

    public long queryTotal(String orderid,String status,String returntime,String aid,String handletime){
        String sql = "select o.* from orderdetail o INNER JOIN shopping_order r ON o.orderid=r.orderid where 1=1";
        ArrayList<Object> params = new ArrayList<>();

        if(orderid != null && orderid.trim().isEmpty() == false){
            sql += " and o.orderid = ?";
            params.add(orderid);
        }

        if(aid != null && aid.trim().isEmpty() == false){
            sql += " and aid = ?";
            params.add(aid);
        }

        if(status != null && status.trim().isEmpty() == false){
            sql += " and o.status = ?";
            params.add(status);
        }

        if(returntime != null && returntime.trim().isEmpty() == false){
            sql += " and returntime between ? and ?";
            params.add(returntime + " 00:00:00");
            params.add(returntime + " 23:59:59");
        }

        if(handletime != null && handletime.trim().isEmpty() == false){
            sql += " and o.handletime between ? and ?";
            params.add(handletime + " 00:00:00");
            params.add(handletime + " 23:59:59");
        }

        return DBHelper.count(sql,params.toArray());

    }

    public List<Map<String,Object>> queryData(String orderid,String status,String returntime,String aid,String handletime, int begin, int size){
        String sql = "select * from orderdetail o INNER JOIN shopping_order r ON o.orderid=r.orderid where 1=1";
        ArrayList<Object> params = new ArrayList<>();
        if(orderid != null && orderid.trim().isEmpty() == false){
            sql += " and o.orderid = ?";
            params.add(orderid);
        }

        if(aid != null && aid.trim().isEmpty() == false){
            sql += " and aid = ?";
            params.add(aid);
        }

        if(status != null && status.trim().isEmpty() == false){
            sql += " and o.status = ?";
            params.add(status);
        }

        if(returntime != null && returntime.trim().isEmpty() == false){
            sql += " and returntime between ? and ?";
            params.add(returntime + " 00:00:00");
            params.add(returntime + " 23:59:59");
        }

        if(handletime != null && handletime.trim().isEmpty() == false){
            sql += " and handletime between ? and ?";
            params.add(handletime + " 00:00:00");
            params.add(handletime + " 23:59:59");
        }

        sql += " limit "+ begin + "," + size;
        return DBHelper.selectListMap(sql,params.toArray());
    }

}
